//
//  JSMSaveViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMSaveViewController.h"
#import "JSMTouringCell.h"
#import "JSMDetailsViewController.h"
@interface JSMSaveViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic ,strong)BaseTableView *saveTableView;
@property(nonatomic, strong)NSArray *arr;
@property(nonatomic, strong)NSMutableArray *touringArr;
@property(nonatomic, strong)NSMutableArray *leavingingArr;
@property(nonatomic, strong)NSMutableArray *dreamCityArr;
@property(nonatomic, strong)NSMutableArray *strategyArr;
@end

@implementation JSMSaveViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        self.arr = @[@"TOURING",@"LAEVING",@"DREAMCITY",@"STRATEGY"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];

    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = LanguageTool(@"我的关注");
    self.touringArr = [SaveTool allSaveModelInPlisttype:SaveTouringModel];
    if (self.touringArr.count + self.leavingingArr.count + self.dreamCityArr.count + self.strategyArr.count == 0 ) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LanguageTool(@"提示") message:LanguageTool(@"还没有收藏")  preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:LanguageTool(@"确定") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:^{
        }];
        
    }
    [self.saveTableView reloadData];
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - TABLEVIEW
- (void) createTableView{
    self.saveTableView = [[BaseTableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.saveTableView];
    [self.saveTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.saveTableView.delegate = self;
    self.saveTableView.dataSource = self;
    self.saveTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.touringArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger num = 0;
    if (self.touringArr.count > 0) {
        num++;
    }
    if (self.leavingingArr.count > 0) {
        num++;
    }
    if (self.dreamCityArr.count > 0) {
        num++;
    }
    if (self.strategyArr.count > 0) {
        num++;
    }
   
    return num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *touringReuse = @"touring";
        JSMTouringCell *cell = [tableView dequeueReusableCellWithIdentifier:touringReuse];
        if (!cell) {
            cell = [[JSMTouringCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:touringReuse];
        }
        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.touringArr[indexPath.row] cover]] placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[[self.touringArr[indexPath.row] userModel] avatar]] placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];
        cell.titleLabel.text = [self.touringArr[indexPath.row] name];
        cell.userNameLabel.text = [NSString stringWithFormat:@"%@ · %@ · %@", [[self.touringArr[indexPath.row] userModel] nickname], [self.touringArr[indexPath.row] destination], [self.touringArr[indexPath.row] start_date]];
        return cell;

    }else{
        static NSString *touringReuse = @"touring";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:touringReuse];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:touringReuse];
        }
        cell.textLabel.text = [self.touringArr[indexPath.row] name];
        return cell;
    }
}
#pragma mark 尺寸
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
#pragma mark 标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.arr[section];
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        JSMDetailsViewController *detaildVc = [[JSMDetailsViewController alloc]init];
        detaildVc.navigationItem.hidesBackButton = YES;
        detaildVc.detailModel = self.touringArr[indexPath.row];
        detaildVc.str_title = [self.touringArr[indexPath.row] destination];
        [self.navigationController pushViewController:detaildVc animated:YES];
    }
}
#pragma mark 左划删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [SaveTool cancelSaveModelInPlist:self.touringArr[indexPath.row] type:SaveTouringModel];
        self.touringArr = [SaveTool allSaveModelInPlisttype:SaveTouringModel];
        [self.saveTableView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
